import React from 'react';
import {Checkbox} from 'antd';
import {WeaSearchGroup, WeaFormItem, WeaInput, WeaSelect} from 'ecCom';
import {WeaLocaleProvider} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import EType from './base/EType';
import ETitle from './base/ETitle';
import EShowNum from './base/EShowNum';
import ELinkMode from './base/ELinkMode';
import EField_New from './base/EField_New'

// 集成登录
export default class Content_outterSys extends React.Component {
    state = {
        eShowField: this.props.eShowField,
        eShowMode: this.props.eShowMode.selected,
        eShowLayout: this.props.eShowLayout.selected,
        eFieldNewDataSource: this.props.eFieldNewDataSource,
        eFieldNewUsedKeys: this.props.eFieldNewUsedKeys,
        eFieldNewVisible: false,
    };

    render() {
        const {eShowField, eShowMode, eShowLayout, eFieldNewDataSource, eFieldNewUsedKeys, eFieldNewVisible} = this.state;

        let EShowField = <div></div>;
        let EShowMode = <div></div>;
        let EShowLayout = <div></div>;
        if (this.props.eShareLevel == '2') {
            let eShowFieldList = [];
            for (let key in eShowField) {
                if (eShowField.hasOwnProperty(key)) {
                    eShowFieldList.push(
                        <li>
                            <Checkbox ecId={`${this && this.props && this.props.ecId || ''}_Checkbox@jbu7m3@${key}`}
                                checked={eShowField[key].checked}
                                onChange={
                                    e => {
                                        this.setState({
                                            eShowField: {
                                                ...eShowField, [key]: {...eShowField[key], checked: e.target.checked}
                                            }
                                        })
                                    }
                                }
                            >
                                {eShowField[key].label}
                            </Checkbox>
                        </li>
                    )
                }
            }

            EShowField = (
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1juljv`} label={getLabel(19495, '显示字段')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                    <ul style={{lineHeight: '23px'}}>
                        {eShowFieldList}
                    </ul>
                </WeaFormItem>
            );

            EShowMode = (
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@rmpfcb`} label={getLabel(21653, '显示方式')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@u6wv96`}
                        options={
                            this.props.eShowMode.options.map((item) => {
                                return {key: item.key, selected: false, showname: item.value};
                            })
                        }
                        value={eShowMode}
                        style={{width: '200px'}}
                        onChange={value => {
                            this.setState({eShowMode: value});
                        }}
                    />
                </WeaFormItem>
            );

            EShowLayout = (
                <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@qs66vl`} label={getLabel(82134, '显示布局')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                    <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@3l5x1j`}
                        options={
                            this.props.eShowLayout.options.map((item) => {
                                return {key: item.key, selected: false, showname: item.value};
                            })
                        }
                        value={eShowLayout}
                        style={{width: '200px'}}
                        onChange={value => {
                            this.setState({eShowLayout: value});
                        }}
                    />
                </WeaFormItem>
            );
        }

        let EFieldNewshow = <div></div>;
        let EFieldNew = <div></div>;
        if (eFieldNewVisible) {
            const eFieldNewProps = {
                eFieldNewDataSource: _.cloneDeep(eFieldNewDataSource),
                eFieldNewUsedKeys: _.cloneDeep(eFieldNewUsedKeys),
                onOk: (data) => {
                    this.setState({
                        eFieldNewDataSource: _.cloneDeep(data.eFieldNewDataSource),
                        eFieldNewUsedKeys: _.cloneDeep(data.eFieldNewUsedKeys),
                        eFieldNewVisible: false
                    });
                },
                onCancel: () => {
                    this.setState({
                        eFieldNewVisible: false
                    });
                }
            };
            EFieldNewshow = (
                <EField_New ecId={`${this && this.props && this.props.ecId || ''}_EField_New@qmqzpg`} {...eFieldNewProps}/>
            );
        }
        const eFieldNewUsedNames = [];
        for (let i = 0; i < eFieldNewUsedKeys.length; i++) {
            for (let j = 0 ; j < eFieldNewDataSource.length; j++) {
                if (eFieldNewUsedKeys[i] == eFieldNewDataSource[j].key) {
                    eFieldNewUsedNames.push(eFieldNewDataSource[j].name);
                }
            }
        }
        
        EFieldNew = this.props.eShareLevel == '2' ? (
            <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@73q43d`} label={getLabel(19495, '显示字段')} labelCol={{span: 6}} wrapperCol={{span: 16}}>
                {EFieldNewshow}
                <div style={{height: '30px', lineHeight: '30px'}}>
                    <i className="icon-coms-Flow-setting portal-esetting-btn" onClick={() => this.setState({eFieldNewVisible: true})}/>
                    <span>{eFieldNewUsedNames.join('，')}</span>
                </div>
            </WeaFormItem>
        ) : '';

        return (
            <div>
                <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@bkplhn`} title={getLabel(81711, '基本信息')} needTigger={true} showGroup={true}>
                    <EType ecId={`${this && this.props && this.props.ecId || ''}_EType@rn334z`} ref="eType" value={this.props.eBaseName}/>
                    {this.props.eShareLevel == '2' ? <ETitle ecId={`${this && this.props && this.props.ecId || ''}_ETitle@hj7nvf`} ref="eTitle" eid={this.props.eid} value={this.props.eTitle} {...this.props}/> : ''}
                    <EShowNum ecId={`${this && this.props && this.props.ecId || ''}_EShowNum@ladso5`} ref="eShowNum" value={this.props.eShowNum}/>
                    <ELinkMode ecId={`${this && this.props && this.props.ecId || ''}_ELinkMode@xbc39p`} ref="eLinkMode" options={this.props.eLinkMode.options} value={this.props.eLinkMode.selected}/>
                    {EFieldNew}
                    {EShowMode}
                    {EShowLayout}
                </WeaSearchGroup>
            </div>
        );
    }

    getSaveValues() {
        return {
            eContentTitle: this.refs['eTitle'] ? this.refs['eTitle'].getSaveValues() : this.props.eTitle,
            eContentShowNum: this.refs['eShowNum'].getSaveValues(),
            eContentLinkMode: this.refs['eLinkMode'].getSaveValues(),
            eContentFieldNewUsedKeys: this.state.eFieldNewUsedKeys,
            eContentShowMode: this.state.eShowMode,
            eContentShowLayout: this.state.eShowLayout
        };
    }
}